$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'pro/stockOut/list',
        datatype: "json",
        colModel: [			
			{ label: '初始数据ID', name: 'id_', width: 11, key: true,hidden:true},
			{ label: 'id', name: 'id', width: 45, key: true,hidden:true},
			{ label: '设备SN/库存数量', name: 'deviceSN', width: 75},
            { label: '品牌', name: 'brand', width: 75},
            { label: '设备名称', name: 'deviceName', width: 75},
			{ label: '物料编码', name: 'materialCode', width: 75 },
			{ label: '设备型号', name: 'typeNo', width: 55 },
			{ label: '规格', name: 'specification', width: 45},
			{ label: '出库检验人id', name: 'stockOutOper', width: 11,hidden:true},
			{ label: '出库检验人', name: 'stockOutOperName', width: 45},
			{ label: '出库时间', name: 'stockOutTime', width: 75},
			{ label: '购买人', name: 'purchaser', width: 45},
			{ label: '购买人电话', name: 'purchaserTel', width: 45},
			{ label: '购买人职务', name: 'purchaserPost', width: 45},
			{ label: '销售人', name: 'saleMan', width: 45},
			{ label: '销售人电话', name: 'saleManTel', width: 45},
			{ label: '接收部门id', name: 'deptId', width: 45,hidden:true},
			{ label: '接收部门', name: 'deptName', width: 45},
			{ label: '库存状态', name: 'stockStatus', width: 45,formatter:function(cellvalue, options, rowObject){  //1:待入库、2:已入库、3:待出库、4:已出库
        		if(rowObject.stockStatus==1){
        			return  '待入库';
        		}else if(rowObject.stockStatus=='2'){
        			return '<span class="label label-success">已入库</span>';
        		}else if(rowObject.stockStatus=='3'){
        			return '待出库';
        		}else if(rowObject.stockStatus=='4'){
        			return '<span class="label label-success">已出库</span>';
        		}
        	}},
			{ label: '备注', name: 'remarks', width: 120}
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    getSelectOpt("deviceType","DEVICE_TYPE",true);
    $('#deviceType').change(function() {
        queryMaterialCode($(this).val());
    }).multipleSelect({
        width: '100%',
		single: true
    });
});

function queryMaterialCode(arr) {
	 var size=0;
	var deviceType="";
	if(!arr||(size=arr.length)===0) {
		return;
	} else {
		deviceType=arr[size-1];
		vm.stockOut.deviceType=deviceType;
	}
	if(deviceType==="pb") {
		$("#device0").show();
		$("#device1").hide();
	} else {
		$("#device1").show();
		$("#device0").hide();
	}
	
	$.ajax({
		type: "GET",
	    url: baseURL + "pro/stock/queryMaterialCode",
       contentType: "application/json",
       data:{"deviceType":deviceType},
	    success: function(r){
			if(r.code == 0){
				vm.typeNoList=r.typeNoList;
				vm.mcList=r.mcList;
			}else{
				alert(r.msg);
			}
		}
	});
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			deviceName: null
		},
		showList: true,
		title:null,
		stockOut:{ 
		},
		typeNoList:[],
		mcList:[],
		deviceSNList:[],
	    deptIdList:[{'value':1,'text':'天下布医'}]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.stockOut = {deviceType:"",typeNo:"",materialCode:"", deviceSN:"",remarks:null};
			$("#materialCode").empty();
			$("#typeNo").empty();
			$("#deviceType").multipleSelect("setSelects", []);
			vm.queryDeviceSNList();
		    $("#sns").multipleSelect({
		    	selectAllText:"全选",
		    	placeholder:"请选择",
		        selectAll: true,
		        checkAll: true,
		        width: "100%"
		    });
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
			vm.getPro(id);
			
		},
		del: function () {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "pro/stockOut/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.stockOut.id_ == null ? "pro/stockOut/save" : "pro/stockOut/update";
			var sns=$("#sns").multipleSelect("getSelects");
			if(!sns || sns.length===0) {
				alert("请选择设备sn号");
				return;
			}
			var arr =$("#deviceType").multipleSelect("getSelects");
			if(arr.length===0) {
				vm.stockOut.deviceType="";
			} else {
				vm.stockOut.deviceType=arr[0];
			}
			vm.stockOut.deviceSN=sns;
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
//			    data: JSON.stringify({"stockOut":vm.stockOut,"sns":sns}),
			    data: JSON.stringify(vm.stockOut),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
/*		queryMaterialCode: function () {
			var arr =$("#deviceType").multipleSelect("getSelects");
			if(arr.length===0) {
				vm.stockOut.deviceType="";
			} else {
				vm.stockOut.deviceType=arr[0];
			}
			$.ajax({
				type: "GET",
			    url: baseURL + "pro/stock/queryMaterialCode",
                contentType: "application/json",
                data:{"deviceType":vm.stockOut.deviceType},
			    success: function(r){
					if(r.code == 0){
						vm.typeNoList=r.typeNoList;
						vm.mcList=r.mcList;
						$("#materialCode").val(r.mcList);
						$("#typeNo").val(r.typeNoList);
					}else{
						alert(r.msg);
					}
				}
			});
		},*/
		queryDeviceSNList: function () {
			$.ajax({
				type: "GET",
				url: baseURL + "pro/stockOut/queryDeviceSNList",
				contentType: "application/json",
//				data:{"deviceType":vm.stockOut.deviceType},
				success: function(r){
					$("#sns").empty();
					if(r.code == 0){
						if(r.sns) {
							$.each(r.sns, function(i, n){
								$("#sns").append(
										"<option value='"+n.value+"'>"+n.text+"</option>"
								);
							});
							$("#sns").multipleSelect("refresh");
							$("#sns").multipleSelect("checkAll");
						}
						
/*						vm.deviceSNList=r.sns;
						$("#sns").val(r.sns);*/
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getPro: function(proId){
			$.get(baseURL + "pro/stockOut/info/"+proId, function(r){
				vm.stock = r.stock;
			});
		},
	 
		reload: function () {
			vm.stockOut = {deviceType:"",typeNo:"",materialCode:"", deviceSN:"",remarks:null};
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'bedNO': vm.q.bedNO},
                page:page
            }).trigger("reloadGrid");
		}
	}
});